package co.fitstart.admin.web.controller.report;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.utils.CollectionUtils;

import co.fitstart.admin.entity.security.Login;
import co.fitstart.admin.service.CourseRoundService;
import co.fitstart.admin.service.PrincipalService;
import co.fitstart.admin.service.SiteService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.admin.web.resolver.Secure;
import co.fitstart.admin.web.view.ExcelTemplateView;
import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.principal.Principal;
import co.fitstart.entity.site.Site;
import co.fitstart.entity.user.Student;

/**
 * 
 * @author lidan
 *
 */
@Controller
@RequestMapping("student-reports")
@MenuSetting("menu-report")
public class StudentReportController {
    
    @Autowired
    private CourseRoundService courseRoundService;
    
    @Autowired
    private SiteService siteService;
    
    @Autowired
    private PrincipalService principalService;
    
    /**
     * 
     * @param login
     * @param siteId
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-student-report")
    public ModelAndView list(@Secure Login login,
                             @RequestParam(name = "site", required = false) Long siteId) {
        
        ModelAndView modelAndView = new ModelAndView("report/student-report-list");
        
        Site site = null;
        
        if(login.hasSite()) {
            site = login.getSite();
        } else {
            if(siteId != null) {
                site = siteService.load(siteId);
            }
            
            if(login.isAreaManager() || login.isAreaServer()) {
                Principal principal = principalService.load(Principal.class, login.getId());
                
                List<Site> sites = siteService.listSite(principal);
                
                if(site == null && !sites.isEmpty()) {
                    site = sites.get(0);
                } else if(sites.isEmpty() || !sites.contains(site)) {
                    site = null;
                }
            } else if(siteId == null) {
                site = siteService.load(1l);
            }
        }
        
        modelAndView.addObject("selectSite", site);
        
        if(site != null) {
            Map<String, List<CourseRound>> courseRounds = courseRoundService.listCourseRound(site);
            
            modelAndView.addObject("courseRounds", courseRounds);
        }
        
        return modelAndView;
    }
    
    /**
     * 
     * @param courseRoundId
     * @return
     */
    @RequestMapping(value = "/{courseRoundId}", method = RequestMethod.GET)
    public ModelAndView view(@PathVariable("courseRoundId") Long courseRoundId) {
        
        ModelAndView modelAndView = new ModelAndView("report/student-report-view");
        
        CourseRound courseRound = courseRoundService.loadCourseRound(courseRoundId);
        
        modelAndView.addObject("courseSchedules", courseRound.getSchedules());
        
        return modelAndView;
    }
    
    /**
     * 
     * @param siteId
     * @return
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView onExport(@RequestParam("site") Long siteId) {
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("studentReport.xls"));
        
        Site site = siteService.load(siteId);
        
        if(site != null) {
            List<CourseRound> courseRounds = courseRoundService.listOfflineCourseRound(site);
            modelAndView.addObject("courseRounds", CollectionUtils.constructMapValuesFromList(courseRounds, "student.id"));
            
            List<Student> students = CollectionUtils.extractPropertyList(courseRounds, "student");
            Map<String, Student> result = CollectionUtils.constructMapValueFromList(students, "id");
            modelAndView.addObject("students", result);
        }
        
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        modelAndView.addObject("fmt", fmt);
        
        return modelAndView;
    }
    

}
